﻿namespace TVersityDotNet.MediaLib
{
    using System.Collections.Generic;
    using System.Globalization;
    using System.Runtime.Serialization;
    using TVersityDotNet.HelperTypes;

    [DataContractAttribute]
    public abstract class MediaLibOptions
    {
        /// <summary>
        /// The id of the object under which the search is to be conducted. Default: 0 the root object
        /// </summary>
        [DataMember]
        public string Id
        {
            get { return this.PropertyBag.GetPropertyFromPropertyBag("id"); }
            set { this.PropertyBag.AddPropertyToPropertyBag("id", value); }
        }

        /// <summary>
        /// The id (typicaly a GUID) of the device profile we should assume. Default: the device profile is automatically detected 
        /// by the user agennt. Unknown user agents receive the "never transcode" device profile
        /// </summary>
        [DataMember]
        public string Pfid
        {
            get { return (string)this.PropertyBag.GetPropertyFromPropertyBag("pfid"); }
            set { this.PropertyBag.AddPropertyToPropertyBag("pfid", value); }
        }

        /// <summary>
        /// The index of the item in the search result set from which the items in the returned document should start
        /// </summary>
        [DataMember]
        public int StartAt
        {
            get { return int.Parse(this.PropertyBag.GetPropertyFromPropertyBag("startat"), CultureInfo.InvariantCulture); }
            set { this.PropertyBag.AddPropertyToPropertyBag("startat", value.ToString(CultureInfo.InvariantCulture)); }
        }

        /// <summary>
        /// The number of items to return. Default is 10, 0 for all
        /// </summary>
        [DataMember]
        public int Count
        {
            get
            {
                int returnNumber = 0;
                int.TryParse(this.PropertyBag.GetPropertyFromPropertyBag("count"), out returnNumber);
                return returnNumber;
            }
            set { this.PropertyBag.AddPropertyToPropertyBag("count", value.ToString(CultureInfo.InvariantCulture)); }
        }

        /// <summary>
        /// The format to be used to return the data (expected is always didlite)
        /// </summary>
        [DataMember]
        protected string Format
        {
            get { return this.PropertyBag.GetPropertyFromPropertyBag("format"); }
            set { this.PropertyBag.AddPropertyToPropertyBag("format", value); }
        }

        /// <summary>
        /// Constructor for MediaLibOptions
        /// </summary>
        public MediaLibOptions()
        {
            this.PropertyBag = new PropertyBag();
        }

        private PropertyBag _propertyBag;
        /// <summary>
        /// A dictionary representing the list of properties that are set. If anything isn't set, the defaults will be used
        /// </summary>
        public PropertyBag PropertyBag
        {
            get
            {
                if (this._propertyBag == null)
                    this._propertyBag = new PropertyBag();
                return this._propertyBag;
            }
            protected set
            {
                this._propertyBag = value;
            }
        }

        /// <summary>
        /// This method is used to ensure all required values are set. If no value is given, the default will be assumed
        /// This is necessary to run if object has been serialized/deserialized as some values might get set in a constructor
        /// that wouldn't be called in that case.
        /// </summary>
        public void EnsureDefaults()
        {
            if (string.IsNullOrEmpty(this.Format))
                this.Format = TVersityResponse.DefaultFormat;
        }
    }
}
